package com.telra.app.server2.service;

import com.telra.app.server2.cassandra.CQLQuery;
import com.telra.app.server2.cassandra.CassandraWriter;
import com.telra.app.server2.jms.JMSPublisherService;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * User: jamesarlet
 * Date: 1/11/12
 * Time: 8:56 PM
 */

@Configuration
@ImportResource("classpath:/spring.xml")

public class ServiceProvider  {

    private static final Log4JLogger log = (Log4JLogger) LogFactory.getFactory().getInstance(ServiceProvider.class);

    private @Value("#{zkProperties.hostname}") String zkhostname;
    private @Value("#{zkProperties.port}")     String zkport;
    private @Value("#{zkProperties.timeout}")  int    zktimeout;
    private @Value("#{zkProperties.cassandra_host}")     String cassandraHostPath;
    private @Value("#{zkProperties.cassandra_port}")     String cassandraPortPath;
    private @Value("#{zkProperties.cassandra_cluster}")  String cassandraClusterPath;
    private @Value("#{zkProperties.cassandra_keyspace}") String cassandraKeyspacePath;
    private @Value("#{zkProperties.mq_url}")   String mqUrl;
    private @Value("#{zkProperties.mq_topic}") String mqTopic;

    public ServiceProvider() {
    }

    public @Bean (name="zookeeperService") ZookeeperService zookeeperService() {
        ZookeeperService zk = new ZookeeperService();
        zk.setHostname(zkhostname);
        zk.setPort(zkport);
        zk.setTimeout(zktimeout);
        zk.setCassandraHostPath(cassandraHostPath);
        zk.setCassandraPortPath(cassandraPortPath);
        zk.setCassandraClusterPath(cassandraClusterPath);
        zk.setCassandraKeyspacePath(cassandraKeyspacePath);
        zk.setMqUrlPath(mqUrl);
        zk.setMqTopicPath(mqTopic);
        zk.init();
        return zk;
    }

    public @Bean (name="cassandraService")
        CassandraService cassandraService() {
        ZookeeperService zk = zookeeperService();
        CassandraService cass = new CassandraService();
        cass.setConfig(zk.getCassandraConfig());
        return cass;
    }

    public @Bean (name="cqlQueryService") CQLQuery cqlQueryService() {
        CassandraService cass = cassandraService();
        CQLQuery cql = new CQLQuery();
        cql.setKeyspace(cass.getKeyspace());
        return cql;
    }

    public @Bean (name="cassandraWriterService") CassandraWriter cassandraWriterService() {
        CassandraService cass = cassandraService();
        CassandraWriter cassw = new CassandraWriter();
        cassw.setKeyspace(cass.getKeyspace());
        return cassw;
    }

    public @Bean (name="esperService") EsperService esperService() {
        return new EsperService();
    }

    public @Bean (name="jmsPublisherService") JMSPublisherService jmsPublisherService() {
        return new JMSPublisherService();
    }

}